package com.youngking.guns.modular.wxuserinfo.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.youngking.guns.core.base.controller.BaseController;
import com.youngking.guns.core.util.ToolUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.youngking.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.youngking.guns.modular.system.model.Wxuserinfo;
import com.youngking.guns.modular.wxuserinfo.service.IWxuserinfoService;

import java.util.Date;

/**
 * 控制器
 *
 * @author fengshuonan
 * @Date 2018-08-03 13:54:35
 */
@Controller
@RequestMapping("/wxuserinfo")
public class WxuserinfoController extends BaseController {

    private String PREFIX = "/wxuserinfo/wxuserinfo/";

    @Autowired
    private IWxuserinfoService wxuserinfoService;

    /**
     * 跳转到首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "wxuserinfo.html";
    }

    /**
     * 跳转到添加
     */
    @RequestMapping("/wxuserinfo_add")
    public String wxuserinfoAdd() {
        return PREFIX + "wxuserinfo_add.html";
    }

    /**
     * 跳转到修改
     */
    @RequestMapping("/wxuserinfo_update/{wxuserinfoId}")
    public String wxuserinfoUpdate(@PathVariable Integer wxuserinfoId, Model model) {
        Wxuserinfo wxuserinfo = wxuserinfoService.selectById(wxuserinfoId);
        model.addAttribute("item", wxuserinfo);
        LogObjectHolder.me().set(wxuserinfo);
        return PREFIX + "wxuserinfo_edit.html";
    }

    /**
     * 获取列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        EntityWrapper ew = new EntityWrapper();
        ew.setEntity(new Wxuserinfo());
        if (ToolUtil.isNotEmpty(condition)) {
            ew.like("nickname", condition).orNew("remark like {0}", condition);
        }
        return wxuserinfoService.selectList(ew);
    }

    /**
     * 新增
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Wxuserinfo wxuserinfo) {
        Wxuserinfo wxuserinfo1old = wxuserinfoService.selectOne(new EntityWrapper<Wxuserinfo>().eq("openid", wxuserinfo.getOpenid()));
        if (wxuserinfo1old != null) {
            wxuserinfo1old.setSubscribe(1);
            wxuserinfo1old.setSubscribeTime(new Date());
            wxuserinfoService.updateById(wxuserinfo1old);
        }else{
            wxuserinfo.setSubscribeTime(new Date());
            wxuserinfoService.insert(wxuserinfo);
        }
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer wxuserinfoId) {
        wxuserinfoService.deleteById(wxuserinfoId);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Wxuserinfo wxuserinfo) {
        wxuserinfoService.updateById(wxuserinfo);
        return SUCCESS_TIP;
    }

    /**
     * 修改关注状态
     */
    @RequestMapping(value = "/updateSubscribe")
    @ResponseBody
    public Object updateSubscribe(String openid, Integer subscribe) {
        Wrapper wrapper = new EntityWrapper();
        wrapper.eq("openid", openid);
        Wxuserinfo wxuserinfo = wxuserinfoService.selectOne(wrapper);
        if (wxuserinfo != null) {
            wxuserinfo.setSubscribe(subscribe);
            wxuserinfoService.updateById(wxuserinfo);
        }
        return SUCCESS_TIP;
    }

    /**
     * 详情
     */
    @RequestMapping(value = "/detail/{wxuserinfoId}")
    @ResponseBody
    public Object detail(@PathVariable("wxuserinfoId") Integer wxuserinfoId) {
        return wxuserinfoService.selectById(wxuserinfoId);
    }
}
